package com.blacktech.dbu.meta.controller;

import com.blacktech.dbu.core.annotation.IgnoreResponseWrap;
import com.blacktech.dbu.meta.repository.*;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/**
 * 统计数据控制器
 *
 * @author Claude
 */
@RestController
@RequestMapping("/api/statistics")
@RequiredArgsConstructor
@CrossOrigin(origins = "*")
public class StatisticsController {

    private final ProductRepository productRepository;
    private final SchemaRepository schemaRepository;
    private final TableRepository tableRepository;
    private final ColumnRepository columnRepository;
    private final ConstraintRepository constraintRepository;
    private final IndexRepository indexRepository;
    private final DictionaryRepository dictionaryRepository;
    private final FieldLibraryRepository fieldLibraryRepository;

    /**
     * 获取系统完整统计数据
     */
    @GetMapping
    @PreAuthorize("hasAnyRole('SUPER_ADMIN', 'ADMIN', 'DEVELOPER', 'VIEWER')")
    public Map<String, Object> getSystemStats() {
        Map<String, Object> stats = new HashMap<>();

        // 获取各项统计数量
        stats.put("products", productRepository.count());
        stats.put("schemas", schemaRepository.count());
        stats.put("tables", tableRepository.count());
        stats.put("columns", columnRepository.count());
        stats.put("constraints", constraintRepository.count());
        stats.put("indexes", indexRepository.count());
        stats.put("dictionaries", dictionaryRepository.count());
        stats.put("fieldLibraries", fieldLibraryRepository.count());

        // 格式化当前时间作为最后更新时间
        String lastUpdate = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        stats.put("lastUpdate", lastUpdate);

        return stats;
    }

    /**
     * 获取产品统计
     */
    @GetMapping("/products")
    @PreAuthorize("hasAnyRole('SUPER_ADMIN', 'ADMIN', 'DEVELOPER', 'VIEWER')")
    public Map<String, Object> getProductStats() {
        Map<String, Object> stats = new HashMap<>();
        stats.put("count", productRepository.count());
        stats.put("lastUpdate", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        return stats;
    }

    /**
     * 获取Schema统计
     */
    @GetMapping("/schemas")
    @PreAuthorize("hasAnyRole('SUPER_ADMIN', 'ADMIN', 'DEVELOPER', 'VIEWER')")
    public Map<String, Object> getSchemaStats() {
        Map<String, Object> stats = new HashMap<>();
        stats.put("count", schemaRepository.count());
        stats.put("lastUpdate", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        return stats;
    }

    /**
     * 获取数据表统计
     */
    @GetMapping("/tables")
    @PreAuthorize("hasAnyRole('SUPER_ADMIN', 'ADMIN', 'DEVELOPER', 'VIEWER')")
    public Map<String, Object> getTableStats() {
        Map<String, Object> stats = new HashMap<>();
        stats.put("count", tableRepository.count());
        stats.put("lastUpdate", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        return stats;
    }

    /**
     * 获取字段统计
     */
    @GetMapping("/columns")
    @PreAuthorize("hasAnyRole('SUPER_ADMIN', 'ADMIN', 'DEVELOPER', 'VIEWER')")
    public Map<String, Object> getColumnStats() {
        Map<String, Object> stats = new HashMap<>();
        stats.put("count", columnRepository.count());
        stats.put("lastUpdate", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        return stats;
    }

    /**
     * 获取约束统计
     */
    @GetMapping("/constraints")
    @PreAuthorize("hasAnyRole('SUPER_ADMIN', 'ADMIN', 'DEVELOPER', 'VIEWER')")
    public Map<String, Object> getConstraintStats() {
        Map<String, Object> stats = new HashMap<>();
        stats.put("count", constraintRepository.count());
        stats.put("lastUpdate", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        return stats;
    }

    /**
     * 获取索引统计
     */
    @GetMapping("/indexes")
    @PreAuthorize("hasAnyRole('SUPER_ADMIN', 'ADMIN', 'DEVELOPER', 'VIEWER')")
    public Map<String, Object> getIndexStats() {
        Map<String, Object> stats = new HashMap<>();
        stats.put("count", indexRepository.count());
        stats.put("lastUpdate", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        return stats;
    }

    /**
     * 获取数据字典统计
     */
    @GetMapping("/dictionaries")
    @PreAuthorize("hasAnyRole('SUPER_ADMIN', 'ADMIN', 'DEVELOPER', 'VIEWER')")
    public Map<String, Object> getDictionaryStats() {
        Map<String, Object> stats = new HashMap<>();
        stats.put("count", dictionaryRepository.count());
        stats.put("lastUpdate", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        return stats;
    }

    /**
     * 获取字段库统计
     */
    @GetMapping("/field-libraries")
    @PreAuthorize("hasAnyRole('SUPER_ADMIN', 'ADMIN', 'DEVELOPER', 'VIEWER')")
    public Map<String, Object> getFieldLibraryStats() {
        Map<String, Object> stats = new HashMap<>();
        stats.put("count", fieldLibraryRepository.count());
        stats.put("lastUpdate", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        return stats;
    }

    /**
     * 健康检查接口 - 不需要统一响应包装
     */
    @GetMapping("/health")
    @IgnoreResponseWrap(reason = "健康检查接口需要原始格式")
    public Map<String, String> health() {
        Map<String, String> status = new HashMap<>();
        status.put("status", "UP");
        status.put("service", "statistics-service");
        status.put("timestamp", String.valueOf(System.currentTimeMillis()));
        return status;
    }
}